import request from '@/utils/request'

// 添加商品
export function add(data) {
    return request({
        url: 'api/goods/save',
        method: 'post',
        data
    })
}
export function edit(data, fileList) {
    let forms = new FormData()
    fileList.forEach(file => {
        console.log(fileList)
        forms.append('fileList', file)
    });
    // 将 data 对象转换为二进制的 blob 对象,
    // 将其作为文件一并与图片上传到后端
    let goods = new Blob([JSON.stringify(data)], {
        type: "application/json"
    });
    forms.append('goods', goods);
    return request({
        url: 'api/goods/update',
        method: 'post',
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        data: forms
    })
}
export function changeStatus(data) {
    return request({
        url: 'api/goods/changeStatus',
        method: 'post',
        data: data
    })
}
export function upload(fileList) {
    let forms = new FormData()
    fileList.forEach(file => {
        forms.append('fileList', file)
    });
    return request({
        url: 'api/goods/upload/',
        method: 'post',
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        data: forms
    })
}
export function del(data) {
    return request({
        url: 'api/goods/delete',
        method: 'delete',
        data
    })
}
// 获取所有商品
export function list(current, size) {
    return request({
        url: 'api/goods/list/' + current + '/' + size,
        method: 'get'
    })
}
export function recently() {
    return request({
        url: 'api/goods/recently/',
        method: 'get'
    })
}
export function filter(data) {
    return request({
        url: 'api/goods/filter',
        method: 'post',
        data
    })
}
export function getOne(id) {
    return request({
        url: 'api/goods/get/'+id,
        method: 'get',
    })
}